import product from "../../js/product"
Page({

    data: {

        p: {},
        ptext: "",
        isShowCamera: false,
        lastCode: "",
        lastScanTime: 0,
        audioErr: null,
        audioOk: null
    },
    onLoad() {

        wx.hideHomeButton({
            success: function(a) {}
        });

        let err = wx.createInnerAudioContext();
        err.src = "/assets/audio/err.mp3"
        let ook = wx.createInnerAudioContext();
        ook.src = "/assets/audio/ok.mp3"
        this.setData({
            audioErr: err,
            audioOk: ook
        })
    },

    onShow() {

    },


    scan: function(params) {
        let isShow = !this.data.isShowCamera;
        this.setData({
            containerPostion: "top",
            isShowCamera: isShow
        })
    },

    leave: function(params) {
        this.setData({
            isShowCamera: false,
            isShowProductDetail: false
        })
    },

    scanRes: function(res) {
        console.log(res);
        let that = this
        let codeStr = res.detail.result
        if (this.data.lastCode == codeStr) {
            if ((new Date().getTime() - this.data.lastScanTime) / 1000 < 3) {
                return
            }
        }

        this.setData({
            lastCode: codeStr,
            lastScanTime: new Date().getTime(),
            ptext: codeStr
        })
        console.log(codeStr);

        that.setData({
            p: {}
        })



        let prodect = product.decode(codeStr)
        if (!prodect) {
            wx.showToast({
                title: '无法解析二维码',
                icon: 'error',
                duration: 1000
            })
            this.data.audioErr.play()
            return
        }

        if (!prodect.skuno) {
            wx.showToast({
                title: '捆包号不存在！',
                icon: 'error',
                duration: 1000
            })
            this.data.audioErr.play()
            return
        }
        if (!prodect.busiinvcode) {
            wx.showToast({
                title: '合同号不存在！',
                icon: 'error',
                duration: 1000
            })
            this.data.audioErr.play()
            return
        }

        let pl = that.data.productList
        that.setData({
            p: prodect
        })
        this.data.audioOk.play()



        wx.showToast({
            title: '扫码成功',
            icon: 'success',
            duration: 2000
        })

    },

    clear: function name(params) {
        this.setData({
            p: {},
            ptext: ""
        })
    }


})